<?php
/**
 * watermark.libaray.php
 * @Copyright lemonCarlJ All rights reserved.
 * @License http://www.56code.com/License.txt
 * @Version 1.0.0
 * @Email rpgshifa@aliyun.com
 */
namespace SF\libaray;

class watermark
{
    //标记
    public $mark = null;
    
    //透明度
    public $transparent = 100;
    
    //旋转角度
    public $angle = 0;
    
    /**
     * 水印开始位置
     * @var integer
     * @desc 按小键盘数字位置
     */
    public $locate = 7;
    
    /**
     * 是否铺满
     * @var integer
     * @desc 如果开启则位置失效
     */
    public $full = 0;
    
    //图片质量
    public $quality = 100;
    
    //字体样式
    public $font_family = SF . 'cour.ttf';
    
    //字体大小
    public $font_size = 12;
    
    //字体颜色
    public $font_color = array( 255, 255, 255 );
    
    /**
     * 初始化水印
     * @param string $mark 水印标记 [ null ]
     */
    public function __construct( $mark = null )
    {
        if( $mark )
        {
            $this->mark = $mark;
        }
    }
    
    /**
     * 创建水印
     * @param resource $source_pic_image 源图片资源
     * @param resource $picture_image 水印图片资源
     * @param array $source_info 源图信息
     * @param array $picture_info 水印图片信息
     * @param string $target_pic 保存地址
     * @return boolean 返回是否创建成功
     */
    protected function create( $source_pic_image, $picture_image, $source_info, $picture_info, $target_pic )
    {
        //是否存在保存操作函数
        $type = image_type_to_extension( $source_info[ 2 ], false );
        $image_function = 'image' . $type;
        if( ! function_exists( $image_function ) )
        {
            return false;
        }
        
        //旋转
        if( $angle = $this->angle )
        {
            //修正背景色
            imagesavealpha( $picture_image, true );
            $image = imagecreatetruecolor( $picture_info[ 0 ], $picture_info[ 1 ] );
            imagealphablending( $image, false );
            imagesavealpha( $image, true );
            imagecopyresampled( $image, $picture_image, 0, 0, 0, 0, $picture_info[ 0 ], $picture_info[ 1 ], $picture_info[ 0 ], $picture_info[ 1 ] );
            imagedestroy( $picture_image );
            $picture_image = $image;
            
            //透明色
            $background_color = imagecolorallocatealpha( $picture_image, 0, 0, 0, 127 );
            $picture_image = imagerotate( $picture_image, $angle, $background_color );
            
            //修正宽高
            $picture_info[ 0 ] = imagesx( $picture_image );
            $picture_info[ 1 ] = imagesy( $picture_image );
        }
        
        //透明度
        $transparent = $this->transparent;
        
        //是否铺满
        if( ! $this->full )
        {
            //生成方位
            switch ( $this->locate )
            {
                //左上
                case 7 :
                    $x_distance = 0;
                    $y_distance = 0;
                    
                    break;
                
                //上中
                case 8 :
                    $x_distance = ( $source_info[ 0 ] - $picture_info[ 0 ] ) / 2;
                    $y_distance = 0;
                    
                    break;
                
                //右上
                case 9 :
                    $x_distance = $source_info[ 0 ] - $picture_info[ 0 ];
                    $y_distance = 0;
                    
                    break;
                
                //左中
                case 4 :
                    $x_distance = 0;
                    $y_distance = ( $source_info[ 1 ] - $picture_info[ 1 ] ) / 2;
                    
                    break;
                
                //中
                case 5 :
                    $x_distance = ( $source_info[ 0 ] - $picture_info[ 0 ] ) / 2;
                    $y_distance = ( $source_info[ 1 ] - $picture_info[ 1 ] ) / 2;
                    
                    break;
                    
                //右中
                case 6 :
                    $x_distance = $source_info[ 0 ] - $picture_info[ 0 ];
                    $y_distance = ( $source_info[ 1 ] - $picture_info[ 1 ] ) / 2;
                    
                    break;
                  
                //左下
                case 1 :
                    $x_distance = 0;
                    $y_distance = $source_info[ 1 ] - $picture_info[ 1 ];
                    
                    break;
                
                //下中
                case 2 :
                    $x_distance = ( $source_info[ 0 ] - $picture_info[ 0 ] ) / 2;
                    $y_distance = $source_info[ 1 ] - $picture_info[ 1 ];
                    
                    break;
                
                //右下
                case 3 :
                    $x_distance = $source_info[ 0 ] - $picture_info[ 0 ];
                    $y_distance = $source_info[ 1 ] - $picture_info[ 1 ];
                    
                    break;
                    
                //随机
                default :
                    $x_distance = mt_rand( 0, $source_info[ 0 ] - $picture_info[ 0 ] );
                    $y_distance = mt_rand( 0, $source_info[ 1 ] - $picture_info[ 1 ] );
                    
                    break;
            }
            
            if( $transparent < 100 && ! $angle )
            {
                imagecopymerge( $source_pic_image, $picture_image, $x_distance, $y_distance, 0, 0, $picture_info[ 0 ], $picture_info[ 1 ], $transparent );
            }else{
                imagecopy( $source_pic_image, $picture_image, $x_distance, $y_distance, 0, 0, $picture_info[ 0 ], $picture_info[ 1 ] );
            }
        
        }else{
            
            //铺满水印
            for( $x_distance = 0; $x_distance < ( $source_info[ 0 ] +  $picture_info[ 0 ] ); $x_distance += $picture_info[ 0 ] )
            {
                for( $y_distance = 0; $y_distance < ( $source_info[ 1 ] +  $picture_info[ 1 ] ); $y_distance += $picture_info[ 1 ] )
                {
                    if( $transparent < 100 && ! $this->angle )
                    {
                        imagecopymerge( $source_pic_image, $picture_image, $x_distance, $y_distance, 0, 0, $picture_info[ 0 ], $picture_info[ 1 ], $transparent );
                    }else{
                        imagecopy( $source_pic_image, $picture_image, $x_distance, $y_distance, 0, 0, $picture_info[ 0 ], $picture_info[ 1 ] );
                    }
                }
            }
            
        }
        
        //生成图片,jpeg可设置图片质量
        if( $type == 'jpeg' )
        {
            $result = $image_function( $source_pic_image, $target_pic, $this->quality );
        }else{
            $result = $image_function( $source_pic_image, $target_pic );
        }
        
        //销毁图片
        imagedestroy( $source_pic_image );
        imagedestroy( $picture_image );
        
        return $result;
    }
    
    /**
     * 印水印[ 图 ]
     * @param string $pic_file 打水印图片地址
     * @param string $target_pic 保存文件地址 [ null ]
     * @return boolean 返回是否打水印成功
     * @desc 如果没有保存文件地址, 则会在源文件上修改
     */
    public function pic( $source_pic, $target_pic = null )
    {
        //水印图片
        $picture = $this->mark;
        if( ! file_exists( $picture ) )
        {
            return false;
        }
        
        //打水印图片
        if( ! file_exists( $source_pic ) )
        {
            return false;
        }
        
        //保存文件地址
        if( ! $target_pic )
        {
            $target_pic = $source_pic;
        }
        
        //资源图片
        if( $source_pic_image = imagecreatefromstring( file_get_contents( $source_pic ) ) )
        {
            //宽高信息
            if( ! ( $source_info = getimagesize( $source_pic ) ) )
            {
                return false;
            }
        }else{
            return false;
        }
        
        //水印图片
        if( $picture_image = imagecreatefromstring( file_get_contents( $picture ) ) )
        {
            //宽高信息
            if( ! ( $picture_info  = getimagesize( $picture ) ) )
            {
                return false;
            }
        }else{
            return false;
        }
        
        return $this->create( $source_pic_image, $picture_image, $source_info, $picture_info, $target_pic );
    }
    
    /**
     * 印水印[ 字 ]
     * @param string $pic_file 打水印图片地址
     * @param string $target_pic 保存文件地址 [ null ]
     * @return boolean 返回是否打水印成功
     * @desc 如果没有保存文件地址, 则会在源文件上修改
     */
    public function word( $source_pic, $target_pic = null )
    {
        //字体库
        $font_family = $this->font_family;
        if( ! file_exists( $font_family ) )
        {
            return false;
        }
        
        //水印文字
        $string = $this->mark;
        if( ! $string )
        {
            return false;
        }
        
        //打水印图片
        if( ! file_exists( $source_pic ) )
        {
            return false;
        }
        
        //保存文件地址
        if( ! $target_pic )
        {
            $target_pic = $source_pic;
        }
        
        //资源图片
        if( $source_pic_image = imagecreatefromstring( file_get_contents( $source_pic ) ) )
        {
            //宽高信息
            if( ! ( $source_info = getimagesize( $source_pic ) ) )
            {
                return false;
            }
        }else{
            return false;
        }
        
        //创建文字图片资源
        $font_size = $this->font_size;
        $font_color = $this->font_color;
        
        $length = mb_strlen( $string );
        $width = mb_strlen( $string ) * $font_size;
        $height = $font_size + 2;
        
        //创建画布
        $image = imagecreatetruecolor( $width, $height );
        imagealphablending( $image, false );
        imagesavealpha( $image, true );
        $background_color = imagecolorallocatealpha($image , 0 , 0 , 0 , 127);
        imagefill( $image, 0, 0, $background_color );
        $color = $color = imagecolorallocate( $image, $font_color[ 0 ], $font_color[ 1 ], $font_color[ 2 ] );
        imagettftext( $image, $font_size, 0, $font_size, $font_size, $color, $font_family, $string );
        
        //参数
        $picture_info[ 0 ] = $width;
        $picture_info[ 1 ] = $height;
        $picture_info[ 2 ] = 2;
        
        return $this->create( $source_pic_image, $image, $source_info, $picture_info, $target_pic );
    }
}